<template>
  <el-dialog
    title="新增字典项"
    width="600px"
    :visible.sync="visible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :show-close="false"
    :append-to-body="true"
  >
    <el-form :model="form" :rules="rules" ref="form">
      <el-form-item label="字典值" prop="code" label-width="80px">
        <el-input v-model="form.code" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="字典标签" prop="value" label-width="80px">
        <el-input v-model="form.value" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="orderNum" label-width="80px">
        <el-input-number v-model="form.orderNum" :min="0"></el-input-number>
      </el-form-item>
      <el-form-item label="备注" label-width="80px">
        <el-input v-model="form.remark" type="textarea" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { mapActions } from 'vuex'
  export default {
    name: 'dictionary-item-add',
    props: {
      visible: {
        type: Boolean,
        default: false
      },
      typeId: {
        type: Number,
        default: null
      }
    },
    data () {
      return {
        form: {
          typeId: '',
          code: '',
          value: '',
          orderNum: '',
          remark: ''
        },
        rules: {
          code: [
            { required: true, message: '请输入字典值', trigger: 'blur' },
            { min: 1, max: 100, message: '最多 100 个字符', trigger: 'blur' }
          ],
          value: [
            { required: true, message: '请输入字典标签', trigger: 'blur' },
            { min: 1, max: 100, message: '最多 100 个字符', trigger: 'blur' }
          ],
          orderNum: [
            { required: true, message: '请输入排序', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      ...mapActions('d2admin/sys.dictionary', [
        'dictItemAdd'
      ]),
      // 取消
      cancel() {
        this.$emit('cancel', false)
      },
      // 提交
      submit() {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.form.typeId = this.typeId
            this.submitHandle(this.form)
          }
        })
      },
      // 提交回调
      submitHandle(form) {
        this.dictItemAdd(form).then(res => {
          this.$emit('submit', false)
        })
      }
    }
  }
</script>

<style scoped>

</style>
